import requests
from requests.auth import HTTPDigestAuth

# import wave
# with wave.open('Get out of here noise reduction v3.3.wav', 'rb') as wav:
#      frame_data = wav.readframes(wav.getnframes())
# #with wave.open('Hawk Screech 1.7.wav', 'rb') as wav:
# #    frame_data = wav.readframes(wav.getnframes())
#
# import base64
# base64_data = base64.b64encode(frame_data)
# print(base64_data)

import base64

def encode_audio_to_base64(audio_file_path):
    """
    Encodes a WAV audio file to a Base64 string.

    Args:
        audio_file_path (str): The path to the WAV audio file.

    Returns:
        str: The Base64 encoded string of the audio file, or None if an error occurs.
    """
    try:
        with open(audio_file_path, "rb") as audio_file:
            encoded_string = base64.b64encode(audio_file.read()).decode('utf-8')
        return encoded_string
    except FileNotFoundError:
        print(f"Error: File not found at {audio_file_path}")
        return None
    except Exception as e:
        print(f"An error occurred: {e}")
        return None

# Example usage:
file_path = 'Get out of here 8000.wav'  # Replace with the actual path to your WAV file
base64_data = encode_audio_to_base64(file_path)
print(base64_data)

# 请求的URL
url = 'http://192.168.50.187/digest/frmSetCustomAudio'

# 请求头
headers = {
    'Content-Type': 'application/json',
    'User-Agent': 'curl/4.7.1',
    'Cache-Control': 'no-cache',
    'Postman-Token': 'cfdf0937-f7a5-4b79-8c39-42eb232d4c0b',
}
print(type(base64_data))
# 请求体
data = {
    "Type": 1,
    "Ch": 1,
    "Data": {
            "DetectType": 9,
            "AudioDataType": 2,
            "AudioData": base64_data
    }
}

# HTTP Digest认证信息
username = 'admin'
password = '123456'  # 密码通常需要填写，但在此示例中未提供

# 发送请求
response = requests.post(url, json=data, headers=headers, auth=HTTPDigestAuth(username, password))
print(response.text)
print(response.status_code)
